$(function () {
    $(".login-form-btnb").click(function () {
        $(location).attr('href', '../html/register.html');
    })
    $(".login-form-btnt").click(function () {
        var $username = $("#username").val()
        var $pass = $("#pass").val();
        if (($username) == "") {
            $(".warn1").css("display", "block");
        }
        if ($pass == "") {
            $(".warn2").css("display", "block");
        }
    })
    $("input").focus(function () {
        if ($(this).val() == "") {
            $(this).siblings(".blankWarn").css("display", "block");
        }
        $("input").keydown(function () {
            $(this).siblings(".blankWarn").css("display", "none");
        })
    })
    $("#eye").click(function () {
        if ($("#pass").attr("type") == "password") {
            $(".ion-eye-disabled").css("display", "none");
            $(".ion-eye").css("display", "block");
            $("#pass").attr("type", "text");
        } else {
            $(".ion-eye-disabled").css("display", "block");
            $(".ion-eye").css("display", "none");
            $("#pass").attr("type", "password");
        }
    })
})

// 点击登录
var loginClick = document.querySelectorAll('.loginClick')[0]
loginClick.addEventListener('click', function () {

    $.ajax({
        type: 'post',
        url: 'http://118.195.129.130:3000/user/login',
        data: {
            us: $("#username").val(),
            ps: $("#pass").val(),
        },
        success: function (data) {
            if (data.err === 0) {
                alert("登录成功！");
                localStorage.setItem("us", $("#username").val());
                localStorage.getItem('us');
                localStorage.setItem("_id", data.data[0]._id);
                localStorage.getItem('_id');
                localStorage.setItem("sex", data.data[0].sex);
                localStorage.getItem('sex');
                localStorage.setItem("age", data.data[0].age);
                localStorage.getItem('age');
                localStorage.setItem("phone", data.data[0].phone);
                localStorage.getItem('phone');
                console.log(data.msg);
                console.log(data.data[0]._id);
                $(location).attr('href', '../html/background_management_system.html');
            } else {
                console.log(data.msg);
                alert("用户名或密码错误！");
            }
        },
        error: function (err) {
            console.log(err);
        }
    })
}, false)
